package com.yanqu.road.entity.activity.sonhai.data;

import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SonHaiPatronsData {

    //门客ID
    private int patronsId;

    //门客职业
    private int occupation;

    //门客赚钱,进攻战力
    private long ability;

    //总血量
    private long allBlood = 0;

    //总掉血
    private long allReBlood = 0;

    //品质
    private int quality = 0;

    //皮肤ID
    private int skinId = 0;

    //是否联动
    private boolean isLinkageSwitch = false;

    //掉血详情MAP
    private Map<Long, SonHaiReBloodInfo> reBloodInfoMap = new ConcurrentHashMap<>();

    public int getPatronsId() {
        return patronsId;
    }

    public void setPatronsId(int patronsId) {
        this.patronsId = patronsId;
    }

    public int getOccupation() {
        return occupation;
    }

    public void setOccupation(int occupation) {
        this.occupation = occupation;
    }

    public long getAbility() {
        return ability;
    }

    public void setAbility(long ability) {
        this.ability = ability;
    }

    /**
     * 获取总掉血
     * @return
     */
    public long getAllReBlood() {
        return this.allReBlood;
    }

    public void setAllReBlood(long reBlood) {
        this.allReBlood = reBlood;
    }

    public long getAllBlood() {
        return allBlood;
    }

    public void setAllBlood(long allBlood) {
        this.allBlood = allBlood;
    }

    public int getQuality() {
        return quality;
    }

    public void setQuality(int quality) {
        this.quality = quality;
    }

    public int getSkinId() {
        return skinId;
    }

    public void setSkinId(int skinId) {
        this.skinId = skinId;
    }

    public boolean isLinkageSwitch() {
        return isLinkageSwitch;
    }

    public void setLinkageSwitch(boolean linkageSwitch) {
        isLinkageSwitch = linkageSwitch;
    }

    public void setReBloodInfoMap(Map<Long, SonHaiReBloodInfo> reBloodInfoMap) {
        this.reBloodInfoMap = reBloodInfoMap;
    }

    public Map<Long, SonHaiReBloodInfo> getReBloodInfoMap() {
        return reBloodInfoMap;
    }

    /**
     * 获取当前血量
     * @return
     */
    public long getBlood() {
        return Math.max(this.allBlood - this.allReBlood, 0);
    }

    /**
     * 添加掉血
     * @param userId
     * @param reBlood
     */
    public synchronized void addReBlood(long userId, long reBlood) {
        //掉血
        this.allReBlood += reBlood;
        //掉血明细
        this.addReBloodInfo(userId, reBlood);
    }

    /**
     * 记录掉血明细
     * @param userId
     * @param reBlood
     */
    public synchronized void addReBloodInfo(long userId, long reBlood) {
        //掉血明细
        SonHaiReBloodInfo info = this.reBloodInfoMap.computeIfAbsent(userId, k -> new SonHaiReBloodInfo());
        info.setUserId(userId);
        info.addReBlood(reBlood);
        info.setTime(System.currentTimeMillis());
    }

    /**
     * 恢复血量
     * @param addBlood
     */
    public synchronized void addBlood(long addBlood) {
        if (addBlood > this.allReBlood) {
            this.allReBlood = 0;
        } else {
            this.allReBlood = this.allReBlood - addBlood;
        }
    }

    /**
     * 清理血量
     */
    public synchronized void clearBlood() {
        this.allReBlood = this.allBlood;
    }

    /**
     * 检查CD中
     * @param userId
     * @return true:cd中，false:没cd中
     */
    public boolean checkHelpCd(long userId, int cd) {
        SonHaiReBloodInfo reBloodInfo = this.reBloodInfoMap.get(userId);
        if (reBloodInfo != null) {
            long cdEndTime = this.getHelpCdEndTime(userId, cd);
            if (cdEndTime >= System.currentTimeMillis()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取CD结束时间
     * @param userId
     * @param cd
     * @return
     */
    public long getHelpCdEndTime(long userId, int cd) {
        long cdTime = cd * DateHelper.SECOND_MILLIONS;
        SonHaiReBloodInfo reBloodInfo = this.reBloodInfoMap.get(userId);
        if (reBloodInfo != null) {
            return reBloodInfo.getTime() + cdTime;
        }
        return 0;
    }
}
